package com.lanou.tipsy.fragment.singer.detail;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.lanou.tipsy.R;
import com.lanou.tipsy.adapter.singer.SDetailSongsAdapter;
import com.lanou.tipsy.base.BaseFragment;
import com.lanou.tipsy.base.Constants;
import com.lanou.tipsy.entity.singer.singers_songs.SSongsData;
import com.lanou.tipsy.entity.singer.singers_songs.SSongsRoot;
import com.lanou.tipsy.tools.listview.NoScrollListView;
import com.lanou.tipsy.tools.volley.ResponseErrorListener;
import com.lanou.tipsy.tools.volley.VolleySingleton;

import java.util.ArrayList;

/**
 * Created by FengJun on 15/10/22.
 * 歌手-歌手详情-歌曲
 */
public class SDetailSongsFragment extends BaseFragment {
    public static final String TAG = "SDetailSongsFragment";
    private NoScrollListView lv_det_songs;
    private SDetailSongsAdapter adapter;
    private VolleySingleton volleySingleton;
    private ResponseErrorListener errorListener;
    private StringRequest request;
    private ArrayList<SSongsData> songsDatas;
    private Context context;
    int urlId = 50530;

    public SDetailSongsFragment() {
        super();
    }

    @SuppressLint("ValidFragment")
    public SDetailSongsFragment(int urlId) {
        this();
        this.urlId = urlId;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fr_singer_det_songs, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //初始化控件的方法
        initView(getView());
        //解析的方法
        analysis();
    }

    private void initView(View view) {
        lv_det_songs = (NoScrollListView) view.findViewById(R.id.lv_det_songs);
        volleySingleton = VolleySingleton.getInstance();
        errorListener = new ResponseErrorListener();
    }

    //解析的方法
    private void analysis() {
        String url = Constants.SINGER_SONGS_START + urlId + Constants.SINGER_SONGS_END;
        request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                SSongsRoot root = JSON.parseObject(response, SSongsRoot.class);
                songsDatas = root.getData();
                adapter = new SDetailSongsAdapter(songsDatas, context);
                lv_det_songs.setAdapter(adapter);
            }
        }, errorListener);
        volleySingleton.addQueue(request, TAG);
    }

    @Override
    public void onStop() {
        super.onStop();
        volleySingleton.removeRequest(TAG);
    }
}
